package model

import (
	"gorm.io/gorm"
	"time"
	"video-srv/basic/config"
)

type Video struct {
	Id          uint32         `gorm:"column:id;type:int UNSIGNED;primaryKey;not null;" json:"id"`
	Title       string         `gorm:"column:title;type:varchar(60);not null;" json:"title"`
	VideoListId int32          `gorm:"column:video_list_id;type:int;not null;" json:"video_list_id"`
	VideoUrl    string         `gorm:"column:video_url;type:varchar(300);not null;" json:"video_url"`
	VideoTypeId int32          `gorm:"column:video_type_id;type:int;not null;" json:"video_type_id"`
	Status      int8           `gorm:"column:status;type:tinyint;not null;" json:"status"`
	CreatedAt   time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt   time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

type VideoList struct {
	Id         uint32         `gorm:"column:id;type:int UNSIGNED;primaryKey;not null;" json:"id"`
	Title      string         `gorm:"column:title;type:varchar(60);not null;" json:"title"`
	VideoCount int8           `gorm:"column:video_count;type:tinyint;not null;" json:"video_count"`
	Status     int8           `gorm:"column:status;type:tinyint;not null;" json:"status"`
	CreatedAt  time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt  time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt  gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

type Protobuf struct {
	Id          uint32         `gorm:"column:id;type:int UNSIGNED;primaryKey;not null;" json:"id"`
	VideoId     int32          `gorm:"column:video_id;type:int;comment:视频id;not null;" json:"video_id"`                        // 视频id
	VideoOrder  int32          `gorm:"column:video_order;type:int;comment:当前剧集数;not null;" json:"video_order"`                 // 当前剧集数
	VideoListId int32          `gorm:"column:video_list_id;type:int;comment:剧集id;not null;" json:"video_list_id"`              // 剧集id
	Status      int8           `gorm:"column:status;type:tinyint;comment:审核状态: 0-待审核 1-审核中 2-通过 3-驳回;not null;" json:"status"` // 审核状态: 0-待审核 1-审核中 2-通过 3-驳回
	CreatedAt   time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt   time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

type User struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;primaryKey;not null;" json:"id"`
	Mobile    string         `gorm:"column:mobile;type:char(11);comment:手机号;not null;" json:"mobile"`    // 手机号
	Password  string         `gorm:"column:password;type:char(32);comment:密码;not null;" json:"password"` // 密码
	CreateAt  time.Time      `gorm:"column:create_at;type:datetime(3);not null;" json:"create_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (u *User) TableName() string {
	return "user"
}

func (u *User) CreateUser() error {
	return config.DB.Create(&u).Error
}

func (u *User) GetUserByMobile() error {
	return config.DB.Where("mobile = ?", u.Mobile).Find(&u).Error
}

func (v *Video) TableName() string {
	return "video"
}

func (v *VideoList) TableName() string {
	return "video_list"
}

func (p *Protobuf) TableName() string {
	return "protobuf"
}

func (p *Protobuf) AddProtobuf() error {
	return config.DB.Create(&p).Error
}

func (p *Protobuf) UpdateProtobuf() error {
	return config.DB.Updates(&p).Error
}

func (v *Video) AddVideo() error {
	return config.DB.Create(&v).Error
}

func (v *VideoList) AddVideoList() error {
	return config.DB.Create(&v).Error
}
